<?php

    namespace app\store\model;

    use think\Model;

    class RegionModel extends Model
    {

        protected $name = 'store_region';

        protected $autoWriteTimestamp = true;

        protected $type = ['create_time' => 'timestamp:Y-m-d H:i','update_time' => 'timestamp:Y-m-d H:i'];

        /**
         * 读取系统设置列表
         *
         * @param array  $condition 检索条件
         * @param string $order     排序条件
         *
         * @return array 数组格式的返回结果
         */
        public function getListArea($condition,$order = '')
        {

            $where  = $this->_condition($condition);
            $field  = isset($condition['field']) ? $condition['field'] : '*';
            $limit  = isset($condition['limit']) ? $condition['limit'] : '';
            $orders = isset($condition['order']) ? $condition['order'] : '';
            if (!empty($order))
            {
                $orders = $order;
            }
            $result = db('storearea')->field($field)->limit($limit)->order($orders)->where($where)->select();
            return $result;
        }

        /**
         * 构造检索条件
         *
         * @param array $condition 检索条件
         *
         * @return string 数组形式的返回结果
         */
        private function _condition($condition)
        {

            $where = '1=1';

            if (isset($condition['storearea_parent_id']) && $condition['storearea_parent_id'] != '')
            {
                $where .= " and storearea_parent_id = '" . intval($condition['storearea_parent_id']) . "'";
            }
            if (isset($condition['storearea_deep']) && $condition['storearea_deep'] != '')
            {
                $where .= " and storearea_deep = '" . intval($condition['storearea_deep']) . "'";
            }
            if (isset($condition['area_hot']) && $condition['area_hot'] != '')
            {
                $where .= " and storearea_hot > 0";
            }
            if (isset($condition['area_deep']) && $condition['area_deep'] != '')
            {
                $where .= " and storearea_deep <= 2";//一级和二级地区
            }
            return $where;
        }

        /**
         * 新增
         *
         * @param array $data 参数内容
         *
         * @return bool 布尔类型的返回结果
         */
        public function addStorearea($data)
        {

            if (empty($data))
            {
                return false;
            }
            if (is_array($data))
            {
                $tmp = [];
                foreach ($data as $k => $v)
                {
                    $tmp[$k] = $v;
                }
                $result = db('storearea')->insertGetId($tmp);
                return $result;
            }
            else
            {
                return false;
            }
        }

        /**
         * 取单个地区的内容
         *
         * @param array $condition 检索条件
         *
         * @return array 数组类型的返回结果
         */
        public function getOneArea($condition)
        {

            if (!empty($condition))
            {

                $result = db('storearea')->where($condition)->find();
                return $result;
            }
            else
            {
                return false;
            }
        }

        /**
         * 更新信息
         *
         * @param array $data 更新数据
         *
         * @return bool 布尔类型的返回结果
         */
        public function editStorearea($data)
        {

            if (empty($data))
            {
                return false;
            }
            if (is_array($data))
            {
                $tmp = [];
                foreach ($data as $k => $v)
                {
                    $tmp[$k] = $v;
                }
                $result = db('storearea')->update($tmp);
                return $result;
            }
            else
            {
                return false;
            }
        }

        /**
         * 删除地区
         *
         * @param int/array $id 删除ID
         * @param int $deep 删除深度,默认为1
         *
         * @return array $rs_row 返回数组形式的查询结果
         */
        public function delStorearea($id,$deep = 1)
        {

            if (!empty($id))
            {
                if (!is_array($id))
                {
                    $id = [$id];
                }

                /**
                 * 取得地区缓存内容
                 */
                $child_deep = $deep + 1;
                for ($i = $child_deep; $i <= 4; $i++)
                {
                    $cache_file = ROOT_PATH . DS . "extend" . DS . "area" . DS . "area_" . $deep . ".php";
                    if (file_exists($cache_file))
                    {
                        require_once($cache_file);
                        $tmp  = 'cache_data_' . $i;
                        $$tmp = $area_array;
                        unset($tmp,$area_array);
                    }
                }
                foreach ($id as $k => $v)
                {
                    if (intval($v) > 0)
                    {
                        $del_tmp[] = "storearea_id = '" . $v . "'";
                        /**
                         * 判断子类中是否还有内容
                         */
                        if ($child_deep <= 4)
                        {
                            $del_parent_id = [$v];
                            for ($i = $child_deep; $i <= 4; $i++)
                            {
                                $tmp = 'cache_data_' . $i;
                                if (isset($$tmp) && is_array($$tmp) && !empty($del_parent_id))
                                {
                                    foreach ($del_parent_id as $k_parent => $v_parent)
                                    {
                                        foreach ($$tmp as $k_2 => $v_2)
                                        {
                                            if ($v_2['storearea_parent_id'] == $v_parent)
                                            {
                                                $del_tmp[]        = "storearea_id = '" . $v_2['storearea_id'] . "'";
                                                $next_parent_id[] = $v_2['storearea_id'];
                                            }
                                        }
                                    }
                                    /**
                                     * 再下一级的父ID
                                     */
                                    $del_parent_id = $next_parent_id;
                                }
                            }
                        }
                    }
                }
                $where  = implode(' or ',$del_tmp);
                $result = db('storearea')->where($where)->delete();
                return $result;
            }
            else
            {
                return false;
            }
        }

        public function area_show()
        {

            $area_one_level              = [];
            $area_two_level              = [];
            $condition                   = [];
            $condition['area_parent_id'] = '1';
            $condition['field']          = 'storearea_id,storearea_name,storearea_parent_id';
            $condition['order']          = 'storearea_parent_id asc,storearea_sort asc,storearea_id asc';
            $area_list                   = $this->getListArea($condition);
            if (is_array($area_list) && !empty($area_list))
            {
                foreach ($area_list as $val)
                {
                    if ($val['storearea_parent_id'] == 0)
                    {
                        $storearea_id                        = $val['storearea_id'];
                        $area_one_level[]                    = $val;
                        $area_two_level[$storearea_id]['id'] = $storearea_id;
                    }
                    else
                    {
                        $storearea_parent_id = $val['storearea_parent_id'];
                        if (isset($area_two_level[$storearea_parent_id]))
                        {
                            $area_two_level[$storearea_parent_id]['children'][] = $val;
                            $area_children                                      = $area_two_level[$storearea_parent_id]['children'];
                            $area_two_level[$storearea_parent_id]['content']    = json_encode($area_children);
                        }
                    }
                }
            }
            return (['area_one_level' => $area_one_level,'area_two_level' => $area_two_level]);
        }
    }